package com.wjb.xietong.ota;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wjb.xietong.R;
import com.wjb.xietong.component.ActManager;
import com.wjb.xietong.component.AppGlobal;
import com.wjb.xietong.view.CustomDialog;


public class BaseFM extends Fragment {
	public AppGlobal app_;
	
	private View layoutView;
	private int index;

	private CustomDialog progressDialog_;
	private Animation am_;
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		app_ = (AppGlobal) getActivity().getApplication();
		app_.setActManager(ActManager.Instance());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		layoutView = inflater.inflate(R.layout.page_fm_template, null);
		return layoutView;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	/** +++++++++++++++++++++++++++设置内容区域+++++++++++++++++++++++++++ */
	protected void setContentField(View v) {
		LinearLayout llContent = (LinearLayout) layoutView.findViewById(R.id.llContent1);

		llContent.removeAllViews();
		llContent.addView(v);
		
		v.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		findView(v);
		setOnClickEvent();
	}
	
	protected void findView(View v) {
	}

	protected void setOnClickEvent() {
	}

	/** +++++++++++++++++++++++++++设置标题栏 区域+++++++++++++++++++++++++++ */
	
	protected void setTopBarAndAction(String title,
			OnClickListener leftBtnListener, OnClickListener rightBtnListener) {
		alabSetBarTitleText(title);
		if (leftBtnListener != null) {
			alabHideButtonLeft(false);
			alabGetButtonLeft().setOnClickListener(leftBtnListener);
		}
		if (rightBtnListener != null) {
			alabHideButtonRight(false);
			alabGetButtonRight().setOnClickListener(rightBtnListener);
		}
	}
	
	protected void setTopBarAndAction(String title,
			OnClickListener leftBtnListener, OnClickListener rightBtnListener, String rightText) {
		this.setTopBarAndAction(title, leftBtnListener, rightBtnListener);
		if(rightText != null){
			alabGetButtonRight().setText(rightText);
		}
	}
	
	protected void setTopBarAndAction(String title,
			OnClickListener leftBtnListener, OnClickListener rightBtnListener, int rightBtnResource) {
		this.setTopBarAndAction(title, leftBtnListener, rightBtnListener);
		if(rightBtnResource != 0 && rightBtnResource != -1) {
			alabGetButtonRight().setBackgroundResource(rightBtnResource);
		}
		
	}
	
	protected void hideTitleBar(){
		RelativeLayout titleBar = (RelativeLayout)layoutView.findViewById(R.id.id_title_bar);
		titleBar.setVisibility(View.GONE);
	}

	private void alabHideButtonLeft(boolean bSetHide) {
		Button bt = alabGetButtonLeft();
		if (null != bt) {
			if (bSetHide)
				bt.setVisibility(View.INVISIBLE);
			else
				bt.setVisibility(View.VISIBLE);
		}
	}

	private void alabHideButtonRight(boolean bSetHide) {
		Button bt = alabGetButtonRight();
		if (null != bt) {
			if (bSetHide)
				bt.setVisibility(View.INVISIBLE);
			else
				bt.setVisibility(View.VISIBLE);
		}
	}

	private Button alabGetButtonLeft() {
		return (Button) layoutView.findViewById(R.id.title_back);
	}

	private Button alabGetButtonRight() {
		return (Button) layoutView.findViewById(R.id.title_home);
	}

	private boolean alabSetBarTitleText(String titleText) {
		TextView tv = (TextView) layoutView.findViewById(R.id.title_txt1);
		if (null != tv) {
			tv.setText(titleText);
			return true;
		}
		return false;
	}
	
	protected enum HttpType{
		GET,
		POST
	}
	
	private DialogProgress dp;
	/**
	 * 显示 waiting 对话框
	 */
	protected void showProgressDialog(){
		
		if(dp == null){
			dp = new DialogProgress(getActivity(), R.style.ProgressDialog);
		}
		dp.show();//显示对话框
		
	}
	
	/**
	 * 隐藏 waiting 对话框
	 */
	protected void hideProgressDialog(){
		
		if(dp != null){
			dp.dismiss();//显示对话框
		}
	}
}
